package com.eqxiu.ieditor.api.controller.sign;

import com.eqxiu.ieditor.api.controller.BaseApiController;
import com.eqxiu.ieditor.api.controller.scene.doc.TagDoc;
import com.eqxiu.ieditor.commons.web.ResultData;
import com.eqxiu.ieditor.interceptor.UserContext;
import com.eqxiu.ieditor.msg.event.HandleSceneStatusEventListener;
import com.eqxiu.user.api.UserExtensionApi;
import com.eqxiu.user.api.dto.User;
import com.eqxiu.user.api.dto.UserSignLogDTO;
import com.google.common.collect.Maps;
import eqxiu.passport.sdk.dto.UserDTO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @description:签到的积分
 * @Author: lifen
 * @Date:  2019/5/17 18:18
 */
@RestController
@RequestMapping("/api/m/sign")
public class SignController implements  BaseApiController {

    private final Logger log = LoggerFactory.getLogger(SignController.class);
    @Autowired
    private UserExtensionApi userExtensionApi;

    @ApiOperation(value = "查询是否已经签到")
    @PostMapping(value = "/checkSignStatus")
    public ResultData checkSignStatus() {
        UserDTO user = UserContext.getUser();
        boolean checkSign=false;
        Map<String,Boolean> signMap= Maps.newHashMap();
        try {
            if (user != null) {
                checkSign= userExtensionApi.checkSignStatus(user.getId());
                signMap.put("checkSign",checkSign);
            }
        } catch (Exception e) {
            log.error("用户签到，userId={}", user.getId(),  e);
        }
        return success().setObj(signMap);
    }
    @ApiOperation(value = "用户签到")
    @PostMapping(value = "/saveUserSignLog")
    public ResultData saveUserSignLog(@ApiParam(value = "签到渠道", required = true) @RequestParam Integer signChannel) {
        UserDTO user = UserContext.getUser();
        UserSignLogDTO userSignLogDTO = new UserSignLogDTO();
        try {
            if (user != null) {
                //签到接口，写入签到记录，增加积分，核算会员等级、权益
                userSignLogDTO = userExtensionApi.saveUserSignLog(user.getId(), user.getLoginName(), signChannel);
                //返回的仅仅是当前加积分的数量
            }
        } catch (Exception e) {
            log.error("用户签到，userId={}, signChannel={}", user.getId(), signChannel, e);
        }
        return success().setObj(userSignLogDTO);
    }
}
